<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="职称ID" prop="tieId">
        <el-select
          v-model="queryParams.tieId"
          placeholder="请选择职称"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in list2"
            :key="dict.id"
            :label="dict.tieName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="序列ID" prop="seqId">
        <el-select
          v-model="queryParams.seqId"
          placeholder="请选择职位序列"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in list3"
            :key="dict.id"
            :label="dict.seqName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属部门" prop="depId">
        <el-tree-select
          v-model="queryParams.depId"
          :data="deptTree"
          :props="defaultProps"
          check-strictly
          default-expand-all
          placeholder="请选择上级部门"
          value-key="deptId"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="工资标准" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入工资标准"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['pm:rules-basepay:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['pm:rules-basepay:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="职称ID" align="center" prop="tieId" >
        <template #default="scope">
          <div v-for="item in list2" :key="item.id">
            <span v-if="item.id === scope.row.tieId">{{ item.tieName }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="序列ID" align="center" prop="seqId" >
        <template #default="scope">
          <div v-for="item in list3" :key="item.id">
            <span v-if="item.id === scope.row.seqId">{{ item.seqName }}</span>
          </div>
        </template> 
      </el-table-column>
      <el-table-column label="所属部门" align="center" prop="depId" >
        <template #default="scope">
          <div v-for="item in list4" :key="item.id">
            <span v-if="item.id === scope.row.depId">{{ item.name }}</span>
          </div>
        </template>
      </el-table-column>  
      <el-table-column label="工资标准" align="center" prop="amount" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['pm:rules-basepay:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['pm:rules-basepay:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <RulesBasepayForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import download from '@/utils/download'
import { RulesBasepayApi, RulesBasepayVO } from '@/api/pm/rulesbasepay'
import RulesBasepayForm from './RulesBasepayForm.vue'
import { ProfessionalTitleApi, ProfessionalTitleVO } from '@/api/pm/professionaltitle'
import { PositionsSeqApi, PositionsSeqVO } from '@/api/pm/positionsseq'
import { defaultProps, handleTree } from '@/utils/tree'
import * as DeptApi from '@/api/system/dept'

/** 基本薪资规则 列表 */
defineOptions({ name: 'RulesBasepay' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<RulesBasepayVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  tieId: undefined,
  seqId: undefined,
  depId: undefined,
  amount: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await RulesBasepayApi.getRulesBasepayPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await RulesBasepayApi.deleteRulesBasepay(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await RulesBasepayApi.exportRulesBasepay(queryParams)
    download.excel(data, '基本薪资规则.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
  getList2()
  getList3()
  getList4()
  getTree()
})

const queryParams2 = reactive({
  pageNo: 1,
  pageSize: 10,
  tieName: undefined,
  tieLevel: undefined,
  tieType: undefined,
})
const list2 = ref<ProfessionalTitleVO[]>([]) // 列表的数据
/** 查询列表 */
const getList2 = async () => {
  try {
    const data = await ProfessionalTitleApi.getProfessionalTitlePage(queryParams2)
    list2.value = data.list
  } finally {
  }
}


const queryParams3 = reactive({
  pageNo: 1,
  pageSize: 10,
  seqName: undefined,
  seqDec: undefined,
  createTime: [],
  remarks: undefined,
})
const list3 = ref<PositionsSeqVO[]>([]) // 列表的数据
/** 查询列表 */
const getList3 = async () => {
  try {
    const data = await PositionsSeqApi.getPositionsSeqPage(queryParams3)
    list3.value = data.list
  } finally {
  }
}


const queryParams4 = reactive({
  pageNo: 1,
  pageSize: 100,
  name: undefined,
  status: undefined
})
const list4 = ref() // 列表的数据
/** 查询部门列表 */
const getList4 = async () => {
  try {
    const data = await DeptApi.getDeptPage(queryParams4)
    list4.value = data
  } finally {
  }
}

const deptTree = ref() // 树形结构
/** 获得部门树 */
const getTree = async () => {
  deptTree.value = []
  const data = await DeptApi.getSimpleDeptList()
  let dept: Tree = { id: 0, name: '顶级部门', children: [] }
  dept.children = handleTree(data)
  deptTree.value.push(dept)
}
</script>